package com.starsky.common.rabbitmq.ack;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @desc: 测试该手动确认方式, 其他 相关监听器可以先注释掉，以免造成多个同类型监听器都监听同一个队列。
 * @author: wangsh
 * @time: 2020/11/13 16:33
 */
@Configuration
public class MessageListenerConfig {

    @Autowired
    private CachingConnectionFactory connectionFactory;
    @Autowired
    private SingleQueuesAckReceiver singleQueuesAckReceiver;//消息接收处理类
    @Autowired
    private MultipleQueuesAckReceiver multipleQueuesAckReceiver;//消息接收处理类

    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        container.setConcurrentConsumers(1);
        container.setMaxConcurrentConsumers(1);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL); // RabbitMQ默认是自动确认，这里改为手动确认消息
        //设置监听一个队列
//        container.setQueueNames("TestDirectQueue");
        //如果同时设置监听多个队列如下： 前提是队列都是必须已经创建存在的
        container.setQueueNames("TestDirectQueue", "topic.man", "topic.woman", "fanout.B", "fanout.A");

        //另一种设置队列的方法,如果使用这种情况,那么要设置多个,就使用addQueues
        //container.setQueues(new Queue("TestDirectQueue",true));
        //container.addQueues(new Queue("TestDirectQueue2",true));
        //container.addQueues(new Queue("TestDirectQueue3",true));
//        container.setMessageListener(singleQueuesAckReceiver);
        container.setMessageListener(multipleQueuesAckReceiver);

        return container;
    }
}
